import { DD_SYSTEM } from '@/constants/api-pre';
import { request } from '@/service/request';

export type Notice = Api.Common.CommonRecord<{
  /** 通知标题 */
  title?: string;
  /** 通知内容 */
  content?: string;
  /** 通知类型 */
  type?: number;
  /** 优先级(L：低，M：中，H：高) */
  level?: number;
  /** 发布人 */
  publisherId?: bigint;
  /** 发布人 */
  publisherName?: string;
  /** 优先级(0-低 1-中 2-高) */
  priority?: number;
  /** 目标类型(0-全体 1-指定) */
  targetType?: number;
  /** 目标ID合集，以,分割 */
  targetUserIds?: number[];
  /** 发布状态(0-未发布 1已发布 2已撤回) */
  publishStatus?: number;
  /** 发布时间 */
  publishTime?: Date;
  /** 撤回时间 */
  revokeTime?: Date;
  /** 创建时间 */
  createTime?: Date;
}>;

export type UserNotice = Api.Common.CommonRecord<{
  id: string;
  /** 通知标题 */
  title?: string;
  /** 通知内容 */
  content?: string;
  /** 通知类型 */
  type: number;
  /** 优先级(L：低，M：中，H：高) */
  level?: number;
  /** 通知类型描述 */
  typeDesc: string;
  /** 发布人 */
  publisherId?: bigint;
  /** 发布人 */
  publisherName?: string;
  /** 优先级(0-低 1-中 2-高) */
  priority?: number;
  /** 目标类型(0-全体 1-指定) */
  targetType?: number;
  /** 发布状态(0-未发布 1已发布 2已撤回) */
  publishStatus?: number;
  /** 发布时间 */
  publishTime?: Date;
  /** 撤回时间 */
  revokeTime?: Date;
  /** 状态 */
  isRead: number;
}>;

export type NoticeEditAndCreateParams = CommonType.RecordNullable<
  Pick<Notice, 'title' | 'content' | 'type' | 'level' | 'targetType' | 'targetUserIds'>
>;

export type NoticeList = Api.Common.PaginatingQueryRecord<Notice>;

export type UserNoticeList = Api.Common.PaginatingQueryRecord<UserNotice>;

export type NoticeSearchParams = CommonType.RecordNullable<
  Pick<Notice, 'publishStatus' | 'title' | 'publishTime'> & Api.Common.CommonSearchParams
>;

export type MyNoticeSearchParams = CommonType.RecordNullable<
  Pick<Notice, 'publishStatus' | 'title' | 'publishTime'> &
    Api.Common.CommonSearchParams & {
      isRead: number;
    }
>;

export function getFetchNoticeUrl(id: number) {
  return `${DD_SYSTEM}/notices/${id}/form`;
}

export function fetchNoticeList(params?: NoticeSearchParams) {
  return request<NoticeList>({
    url: `${DD_SYSTEM}/notices/page`,
    method: 'get',
    params
  });
}
export function createNotice(data: any) {
  return request<boolean>({
    url: `${DD_SYSTEM}/notices`,
    method: 'post',
    data
  });
}
export function updateNotice(id: number, data?: any) {
  return request<boolean>({
    url: `${DD_SYSTEM}/notices/${id}`,
    method: 'put',
    data
  });
}
export function deleteNotice(id: string) {
  return request<boolean>({
    url: `${DD_SYSTEM}/notices/${id}`,
    method: 'delete'
  });
}
export function publishNotice(id: number) {
  return request<boolean>({
    url: `${DD_SYSTEM}/notices/${id}/publish`,
    method: 'patch'
  });
}
export function revokeNotice(id: number) {
  return request<boolean>({
    url: `${DD_SYSTEM}/notices/${id}/revoke`,
    method: 'patch'
  });
}
export function fetchNoticeDetail(id: number) {
  return request<Notice>({
    url: `${DD_SYSTEM}/notices/${id}/detail`,
    method: 'get'
  });
}
export function readAllNotice() {
  return request<boolean>({
    url: `${DD_SYSTEM}/notices/read-all`,
    method: 'put'
  });
}
export function fetchMyNoticePage(params?: MyNoticeSearchParams) {
  return request<UserNoticeList>({
    url: `${DD_SYSTEM}/notices/my-page`,
    method: 'get',
    params
  });
}
